package com.express.controller;


import com.express.auto.JSONAuthentication;
import com.express.entity.ExCar;
import com.express.entity.ExOrder;
import com.express.mapper.ExOrderMapper;
import com.express.response.Result;
import com.express.service.ExOrderService;
import com.express.utils.BaiDuApiUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.sql.Timestamp;
import java.util.Date;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author express
 * @since 2022-01-23
 */
@RestController
@RequestMapping("/ex-order")
public class ExOrderController extends JSONAuthentication {

    @Autowired
    private ExOrderMapper exOrderMapper;
    @Autowired
    private ExOrderService exOrderService;
    @ApiOperation(value = "添加订单")
    @ApiImplicitParam (value = "整个订单参数")
    @PostMapping("/add")
    public void addOrder(HttpServletRequest request, HttpServletResponse response, @RequestBody ExOrder exOrder) throws IOException, ServletException {

        System.out.println(exOrder);

        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        exOrder.setCreatTime(timestamp);

        int insert = exOrderMapper.insert(exOrder);
        if (insert > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除订单")
    @ApiImplicitParam(value = "订单id")
    @PostMapping("/del")
    public void delOrder(HttpServletRequest request, HttpServletResponse response, @RequestParam("orderId") int orderId) throws IOException, ServletException {
        int i = exOrderMapper.deleteById(orderId);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看订单")
    @PostMapping("/look")
    public void lookOrder(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
        Result ok = Result.ok().data("ok",exOrderService.findAll());
        this.WriteJSON(request, response, ok);
    }
    @ApiOperation(value = "根据订单号修改订单状态")
    @PostMapping("/upd")
    public void updCar(HttpServletRequest request, HttpServletResponse response, @RequestBody ExOrder exOrder) throws IOException, ServletException {
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        exOrder.setUpdTime(timestamp);
        int i = exOrderMapper.updStateByid(exOrder.getOrderId(),exOrder.getState());
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }

    }
    @ApiOperation(value = "根据地址改变查询出价格")
    @ApiImplicitParam(value = "起始地，目的地，物品重量")
    @PostMapping("/lookprice")

    public void getprice(HttpServletRequest request, HttpServletResponse response, @RequestParam("startAddress") String startAddress,@RequestParam("endAddress") String endAddress ,@RequestParam("goodsWight") Double goodsWight){
        //获得起始地 终点  物品重量计算出订单价格

        //通过地址获取到经纬度

        try {
            String start =new BaiDuApiUtils().getCoordinate(startAddress);
            String end=new BaiDuApiUtils().getCoordinate(endAddress);
            //根据经纬度计算出距离
            Double juli=new BaiDuApiUtils().getDistance(start,end);
            //根据距离 重量查询出需要多少钱
            Double price =juli/40000*goodsWight;;
            //快递价格不满10元按10元计算
            if(price<=10){
                price=10.0;
            }
            //返回出去
            Result result=Result.ok().data("price",price);
            result.data("distance",juli);
            this.WriteJSON(request,response,result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}

